﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NeuralStock.DomainService.Interfaces;
using NeuralStock.Entities;
using NeuralStock.Common;
using NeuralStock.Tools;

namespace NeuralStock.DomainService.Implementation
{
    public class MoneyImportService : IMoneyImportService
    {
        ICompanyService companyService;
        IFinancialReportService financialReportService;

        public MoneyImportService(ICompanyService companyService, IFinancialReportService financialReportService)
        {
            this.companyService = companyService;
            this.financialReportService = financialReportService;
        }

        public void ImportData()
        {
            var moneyReports = GetData();

            foreach (var reportData in moneyReports)
            {
                reportData.CompanyId = companyService.Get(reportData.CompanyName).Id;

                var existingReport = financialReportService.Get(reportData.CompanyId.Value, reportData.Year.Value);
                if (existingReport == null)
                {
                    financialReportService.Create(reportData);
                    Console.WriteLine(string.Format("Created: {0} {1}", reportData.CompanyName, reportData.Year.ToString()));
                }
                else
                {
                    financialReportService.Update(existingReport.Id, reportData);
                    Console.WriteLine(string.Format("Updated: {0} {1}", reportData.CompanyName, reportData.Year.ToString()));
                }
            }
        }

        public List<FinancialReportData> GetData()
        {
            MoneyCsvReader csvReader = new MoneyCsvReader();
            return csvReader.GetData();
        }
    }
}
